package stackoverflow.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import stackoverflow.domain.question.Question;
import stackoverflow.domain.service.QuestionService;

@Controller("viewQuestionController")
@RequestMapping("/viewQuestions")
public class ViewQuestionController
{
	private QuestionService questionService;
	
	@Autowired
	public ViewQuestionController(@Qualifier("questionServiceImpl") QuestionService questionService)
	{
		this.questionService = questionService;
	}
	
	@Resource(name="questionServiceImpl")
	public void setQuestionService(QuestionService questionService)
	{
		this.questionService = questionService;
	}	

	@RequestMapping(value="/viewQuestions.htm",method=RequestMethod.GET)
	public ModelAndView viewQuestions(HttpServletRequest req,
			HttpServletResponse res) throws Exception
	{
		List<Question> questions = questionService.viewQuestions();
		
		ModelAndView mv = new ModelAndView();
		Map<String, Object> modelMap = new HashMap<String, Object>();        
        modelMap.put("questions", questions);
        //mv.setViewName("public/viewQuestions");
        mv.setViewName("viewQuestions");
        mv.addAllObjects(modelMap);
        return mv;	
	}
	
	
	@RequestMapping(value="/addQuestionForm",method=RequestMethod.GET)
	public String gotoAddQuestionForm()
	{
		return "addQuestionForm";
//		return "usersOnly/addQuestionForm";
	}
	
//	public ModelAndView viewQuestionsAskedLastWeek(HttpServletRequest req, HttpServletResponse res) throws Exception
//	{
//		questionService.getQuestionsAskedSinceLastWeek();
//		return new ModelAndView("viewQuestions");
//	}
//
//	public ModelAndView viewQuestionsAskedLastMonth(HttpServletRequest req, HttpServletResponse res) throws Exception
//	{
//		List<Question> questions = questionService.getQuestionsAskedSinceLastMonth();
//		return new ModelAndView("viewQuestions");
//	}
}
